package com.shen.service;

import com.shen.pojo.Student;
import com.shen.util.BaseDao;

import java.util.List;

/**
 * Author:shen
 * CreateDate:2020/7/2.
 * Function:
 */
public class StudentDaoImpl implements StudentDao {

    BaseDao baseDao=new BaseDao();
    @Override
    public int insertStudent(Student student) {
        String sql="insert into `s_student`(`sname`,`sex`,`telephone`,`qq`,`wechat`,`gradeid`,`enterdate`,`fromcity`,`birthday`,`address`,`pass`,`idcardpic`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
        int sex = getSex(student.getSex());
        Object[] objects={
                student.getSname(),
                sex,
                student.getTelephone(),
                student.getQq(),
                student.getWechat(),
                student.getGradeid(),
                student.getEnterdate(),
                student.getFromcity(),
                student.getBirthday(),
                student.getAddress(),
                student.getPass(),
                student.getIdcardpic()

        };
        int i = baseDao.executeUpdate(sql, objects);
        return i;
    }
    public int getSex(String sex) {
        int sexNum=1;
        if(sex.equals("男")){
            sexNum= 1;
        }else if(sex.equals("女")){
            sexNum=2;
        }else if(sex.equals("人妖")){
           sexNum=3;
        }
        return sexNum;
    }
    @Override
    public Student selectStudentById(int id) {
        String sql="SELECT * FROM s_student AS s WHERE s.`stuno`=?";
        Object[] obj={id};
        Student singleResult = baseDao.getSingleResult(sql, obj, Student.class);
        return singleResult;
    }

    @Override
    public int updateStudent(Student student) {
        String sql="update `s_student` set `sname`=?,`sex`=?,`telephone`=?,`qq`=?,`wechat`=?,`gradeid`=?,`enterdate`=?,`fromcity`=?,`birthday`=?,`address`=?,`pass`=?,`idcardpic`=? where stuno=?;";
        int sex = getSex(student.getSex());
        Object[] objects={
                student.getSname(),
                sex,
                student.getTelephone(),
                student.getQq(),
                student.getWechat(),
                student.getGradeid(),
                student.getEnterdate(),
                student.getFromcity(),
                student.getBirthday(),
                student.getAddress(),
                student.getPass(),
                student.getIdcardpic(),
                student.getStuno(),
        };
        int i = baseDao.executeUpdate(sql, objects);
        return i;
    }

    @Override
    public List<Student> getAllStudent(int pageNum, int pageSize) {
        StringBuffer sql=new StringBuffer("SELECT * FROM s_student AS s ");
        List<Student> complexResult;
        if(pageNum>0&&pageSize>0){
            sql.append("LIMIT ?,?;");
            int total=pageNum*pageSize;
            pageNum=total-pageSize;
            Object[] objects={pageNum,pageSize};
            complexResult = baseDao.getComplexResult(sql.toString(),objects,Student.class);
        }else{
           complexResult = baseDao.getComplexResult(sql.toString(),null,Student.class);
        }
        return complexResult;
    }
}
